<?php
namespace app\service;

use app\model\DistributionOfficerModel;
use app\model\MemberModel;
use app\model\MembersRelationModel;
use app\model\MemberWalletLogModel;
use app\model\MemberWalletModel;
use app\model\OrderDistributionModel;
use const support\business_types_1;
use const support\business_types_2;
use const support\CORRECT;
use const support\DENY;
use const support\direction_in;
use const support\direction_out;
use const support\PARAMETER_ERROR;
use const support\wallet_types_1;

class OrderDistributionService
{

    //查询我的分销订单
    public static function getOrderDistribution(int $memberId,int $type,int $page,int $limit): array
    {
        $obj=OrderDistributionModel::where([
            'member_promotion_id'=>$memberId
        ])->orderBy('id','desc');
        if ($type==1 || $type==2){
            $obj=$obj->where([
                'status'=>CORRECT
            ])->where('is_member_promotion_settlement',$type);
        }else{
            $obj=$obj->where([
                'status'=>DENY
            ]);
        }
        $totalNum=$obj->count();
        $offset=($page-1)*$limit;
        $orders=$obj->with([
            'orders'=>function($query){
                $query->select([
                    'id','order_sn','goods_id','goods_title','goods_icon','pay_time','create_time','goods_period_date','join_member_nums','update_time'
                ]);
            }
        ])->offset($offset)->limit($limit)->select([
            'order_id','order_promotion_amount','member_promotion_settlement_time','status'
        ])->get()->toArray();
        $orders=array_map(function($row) use($type){
            return [
                'order_id'=>$row['order_id']??'',
                'order_sn'=>$row['orders']['order_sn']??'',
                'goods_id'=>$row['orders']['goods_id']??'',
                'goods_title'=>$row['orders']['goods_title']??'',
                'goods_icon'=>$row['orders']['goods_icon']??'',
                'pay_time'=>$row['orders']['pay_time']??0,
                'create_time'=>$row['orders']['create_time']??0,
                'goods_period_date'=>$row['orders']['goods_period_date']??0,
                'join_member_nums'=>$row['orders']['join_member_nums']??[],
                'order_promotion_amount'=>$row['order_promotion_amount']??0,
                'member_promotion_settlement_time'=>$row['member_promotion_settlement_time']??0,
                'order_cancel_time'=>$row['orders']['update_time']??0,
                'order_type'=>$type
            ];
        },$orders);
        return commonPagingData($orders,$page,$limit,$totalNum);
    }

    //分销中心数据
    public static function getUserDistributionCenterData(int $memberId): array
    {
        $data=[];
        //用户头像、真实姓名、ID
        $officer=DistributionOfficerModel::where([
            'member_id'=>$memberId,
            'status'   =>CORRECT
        ])->first();
        if (empty($officer->id)){
            return formatReturnData(PARAMETER_ERROR,'该用户不是分销官');
        }
        $officer=$officer->toArray();
        //获取用户头像昵称信息
        $member=MemberModel::where([
            'id'=>$memberId,
            'status'   =>CORRECT
        ])->select(['member_sn','nickname','avatar'])->first()->toArray();
        $data['member_sn']=$member['member_sn'];
        $data['nickname']=$member['nickname']?:'微信用户';
        $data['avatar']=$member['avatar']?:sprintf("%s/image/default.png",env('APP_URL'));
        $data['real_name']=$officer['real_name'];
        $data['mobile']=$officer['mobile'];
        //已提现、待分成、可提现
        $withdrawAmount=MemberWalletLogModel::where([
            'wallet_type'   =>wallet_types_1,
            'member_id'     =>$memberId,
            'business_type' =>business_types_2,
            'direction'     =>direction_out
        ])->sum('change_amount');
        $data['withdraw_amount']=number_format($withdrawAmount, 2, '.', '');
        $memberWallet=MemberWalletModel::where([
            'wallet_type'   =>wallet_types_1,
            'member_id'     =>$memberId,
            'status'        =>CORRECT
        ])->first();
        $withdrawableAmount=0;
        if (!empty($memberWallet->id)){
            $withdrawableAmount=$memberWallet->available_balance;
        }
        $data['withdrawable_amount']=number_format($withdrawableAmount, 2, '.', '');
        $preCommissionAmount=OrderDistributionModel::where([
            'member_promotion_id'=>$memberId,
            'status'=>CORRECT,
            'is_member_promotion_settlement'=>DENY
        ])->sum('order_promotion_amount');
        $data['pre_commission_amount']=number_format($preCommissionAmount, 2, '.', '');
        //我的邀请
        $data['invite_num']=MembersRelationModel::where([
            'pid'=>$memberId,
            'status'=>CORRECT
        ])->count();
        //分销订单
        $data['invite_order_num']=OrderDistributionModel::where([
            'member_promotion_id'=>$memberId,
            'status'=>CORRECT
        ])->count();
        //佣金明细
        $data['commission_num']=MemberWalletLogModel::where([
            'wallet_type'   =>wallet_types_1,
            'member_id'     =>$memberId,
            'business_type' =>business_types_1,
            'direction'     =>direction_in
        ])->count();
        //提现明细
        $data['withdraw_num']=MemberWalletLogModel::where([
            'wallet_type'   =>wallet_types_1,
            'member_id'     =>$memberId,
            'business_type' =>business_types_2,
            'direction'     =>direction_out
        ])->count();
        return $data;
    }

}